package com.neusoft.ruangong.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.ruangong.domain.Room;
import com.neusoft.ruangong.other.utils.R;
import com.neusoft.ruangong.service.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
@Api(tags = {"会议室模块"})
@CrossOrigin(origins ="*")
@RestController
@RequestMapping("/room")
public class RoomController {
    @Resource
    RoomService roomService;
    @ApiOperation("查询所有会议室")
    @GetMapping("/findAllRoom")
    public R queryRooms(){
        List<Room> rooms = roomService.queryRooms();
        if(rooms!=null){
            return R.ok().data("rooms",rooms);
        }else {
            return R.error();
        }
    }
    @ApiOperation("根据id查询会议室")
    @GetMapping("/findRoom")
    public R queryRoom(BigInteger id){
        System.out.println(id);
        Room room = roomService.queryRoomById(id);
        if(room!=null){
            return R.ok().data("room",room);
        }else {
            return R.error();
        }
    }
    @ApiOperation("模糊查询会议室")
    @GetMapping("/linkFindRoom")
    public R linkFindRoom(String str){
        List<Room> list = roomService.linkQueryRooms(str);
        System.out.println(list.size());
        if(list.size()==0){
            return R.error();
        }
        return R.ok().data("list",list);
    }
    @ApiOperation("分页查询会议室")
//    @GetMapping(value = "/pageRoom/{index}/{limit}")
    @GetMapping(value = "/pageRoom")
    public R pageRoom(@RequestParam("page") Integer index,
                      @RequestParam("limit") Integer limit){
        Page<Room> roomPage = new Page<>(index,limit);
        Page<Room> page = roomService.page(roomPage, null);
        List<Room> records = page.getRecords();
        Integer length = roomService.count();
        return R.ok().data("data",records).data("length",length);
    }
    @ApiOperation("添加会议室")
    @PostMapping("/addRoom")
    public R addRoom(@RequestBody Room room){
        Integer result = roomService.addRoom(room);
        if(result==1){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("根据id删除会议室")
    @DeleteMapping("/remove")
    public R removeRoom(Long id){
        Integer result = roomService.removeRoom(id);
        if(result==1){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("修改会议室")
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    public R modifyRoom(@RequestBody Room room){
        Integer result = roomService.modifyRoom(room);
        if(result==1){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
